/**
 * Created by guojianyu on 2016/3/30.
 */
myApp.config(function ($routeProvider) {
    $routeProvider.when('/myFood/view', {
        templateUrl: 'myFood/view',
        controller: 'myFoodController'
    })
});
myApp.service('myFoodListService', function ($http) {
    var doRequest = function () {
        return $http.get('myFood/list');
    }
    return {
        myFoodList: function () {
            return doRequest();
        }
    };
});
myApp.controller('myFoodController', function ($scope, $location, $http, myFoodListService,Notify) {
    /*获取表单数据*/
    myFoodListService.myFoodList().success(function (response) {
        $scope.myFoods = response;
    });
    /*删除餐品*/
    $scope.deleteFood = function (id, name) {
        if (confirm('确定要删除"' + name + '"吗?')) {
            $http.get('myFood/delete/' + id).success(function () {
                Notify.success('删除成功');
                /*重新获取页面数据目的刷新refresh*/
                myFoodListService.myFoodList().success(function (response) {
                    $scope.myFoods = response;
                });
            }).error(function () {
                Notify.error('删除失败');
            });
        }
    };
    /*增加餐品*/
    $scope.addFood=function(){
        $scope.myFood={};
    };
    /*修改餐品*/
    $scope.updateFood=function(data){
        $scope.myFood =angular.copy(data);
    };
    $scope.saveFood = function () {
        $http.post('myFood/edit', $scope.myFood).success(function () {
            Notify.success("success");
            /*重新获取页面数据目的刷新refresh*/
            myFoodListService.myFoodList().success(function (response) {
                $scope.myFoods = response;
            });
        }).error(function () {
            Notify.error('失败');
        });
    };
    /*返回登录页*/
    $scope.return=function() {
        window.location.href = "/web/"
    }
});

